package learn.up.java.thread.threads;

import java.util.ArrayList;
import java.util.List;

/**
 * 程序开始，A线程判断盘子是否为空，放入一个鸡蛋，并且唤醒在阻塞队列的一个线程，
 * 阻塞队列为空；假设CPU又调度了一个A线程，盘子非空，执行等待，这个A线程进入阻
 * 塞队列；然后一个B线程执行，盘子非空，取走鸡蛋，并唤醒阻塞队列的A线程，A线程进
 * 入就绪队列，此时就绪队列就一个A线程，马上执行，放入鸡蛋；如果再来A线程重复第
 * 一步，在来B线程重复第二步，整个过程就是生产者(A线程)生产鸡蛋，消费者(B线程)消费鸡蛋。
 * @author NANUTO
 * @date 2017年12月7日 下午11:40:37
 */
public class Plate {
	
	public static void main(String[] args) {
		 Plate plate = new Plate();  
	        for(int i = 0; i < 10; i++) {  
	            new Thread(new GetThread(plate)).start();  
	            new Thread(new AddThread(plate)).start();  
	        }  
	}

	List<Object> plate = new ArrayList<>();

	public synchronized void getEgg() {
		while (plate.size() == 0) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		plate.clear();
		notifyAll();
		System.out.println("get egg");
	}
	
	public synchronized void putEgg(Object egg) {
		while (plate.size() == 1) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		plate.add(egg);
		notifyAll();
		System.out.println("put egg");
	}
}
class AddThread implements Runnable {
	private Plate plate;
	private Object egg = new Object();  
	public AddThread(Plate plate) {
		this.plate = plate;
	}
	@Override
	public void run() {
		plate.putEgg(egg);
	}
}
class GetThread implements Runnable {
	private Plate plate;
	public GetThread(Plate plate) {
		this.plate = plate;
	}

	@Override
	public void run() {
		plate.getEgg();
	}
}
